/**
 * @a https://leetcode.cn/problems/minimum-number-of-frogs-croaking/
 */

#include "common.h"


class Solution
{
public:
    int minNumberOfFrogs(string croakOfFrogs)
    {
        int ret = -1;
        int croak_perchar_count[256] = {0}; // Initialize all to 0

        for (auto ch : croakOfFrogs)
        {
            if (ch == 'c')
            {
                croak_perchar_count['c']++;
            }
            else if (ch == 'r')
            {
                if (croak_perchar_count['c'] == 0)
                    return -1;
                croak_perchar_count['r']++;
                croak_perchar_count['c']--;
            }
            else if (ch == 'o')
            {
                if (croak_perchar_count['r'] == 0)
                    return -1;
                croak_perchar_count['o']++;
                croak_perchar_count['r']--;
            }
            else if (ch == 'a')
            {
                if (croak_perchar_count['o'] == 0)
                    return -1;
                croak_perchar_count['a']++;
                croak_perchar_count['o']--;
            }
            else if (ch == 'k')
            {
                if (croak_perchar_count['a'] == 0)
                    return -1;
                croak_perchar_count['a']--;
            }
            else
            {
                return -1;
            }
            ret = max(ret,croak_perchar_count['c'] + croak_perchar_count['r'] +croak_perchar_count['o'] + croak_perchar_count['a'] + croak_perchar_count['k']);
        }

        // After processing all characters, check if all counts are zero except 'k' which is not tracked
        if (croak_perchar_count['c'] != 0 || croak_perchar_count['r'] != 0 ||
            croak_perchar_count['o'] != 0 || croak_perchar_count['a'] != 0)
        {
            return -1;
        }

        return ret;
    }
};